home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_emacs.idb / usr / freeware / info / cl-1.z / cl-1 (.txt)
GNU Info File  |  1998-10-27  |  47KB  |  813 lines

  1. This is Info file ../info/cl, produced by Makeinfo-1.64 from the input
  2. file cl.texi.
  3.    This file documents the GNU Emacs Common Lisp emulation package.
  4.    Copyright (C) 1993 Free Software Foundation, Inc.
  5.    Permission is granted to make and distribute verbatim copies of this
  6. manual provided the copyright notice and this permission notice are
  7. preserved on all copies.
  8.    Permission is granted to copy and distribute modified versions of
  9. this manual under the conditions for verbatim copying, provided also
  10. that the section entitled "GNU General Public License" is included
  11. exactly as in the original, and provided that the entire resulting
  12. derived work is distributed under the terms of a permission notice
  13. identical to this one.
  14.    Permission is granted to copy and distribute translations of this
  15. manual into another language, under the above conditions for modified
  16. versions, except that the section entitled "GNU General Public License"
  17. may be included in a translation approved by the author instead of in
  18. the original English.
  19. File: cl,  Node: Top,  Next: Overview,  Up: (dir)
  20. Common Lisp Extensions
  21. **********************
  22. This document describes a set of Emacs Lisp facilities borrowed from
  23. Common Lisp.  All the facilities are described here in detail; for more
  24. discussion and examples, Guy L. Steele's `Common Lisp, the Language',
  25. second edition, is the definitive book on Common Lisp.  While this
  26. document does not assume any prior knowledge of Common Lisp, it does
  27. assume a basic familiarity with Emacs Lisp.
  28. * Menu:
  29. * Overview::             Installation, usage, etc.
  30. * Program Structure::    Arglists, `eval-when', `defalias'
  31. * Predicates::           `typep', `eql', and `equalp'
  32. * Control Structure::    `setf', `when', `do', `loop', etc.
  33. * Macros::               Destructuring, `define-compiler-macro'
  34. * Declarations::         `proclaim', `declare', etc.
  35. * Symbols::              Property lists, `gensym'
  36. * Numbers::              Predicates, functions, random numbers
  37. * Sequences::            Mapping, functions, searching, sorting
  38. * Lists::                `cadr', `sublis', `member*', `assoc*', etc.
  39. * Hash Tables::          `make-hash-table', `gethash', etc.
  40. * Structures::           `defstruct'
  41. * Assertions::           `check-type', `assert', `ignore-errors'.
  42. * Efficiency Concerns::         Hints and techniques
  43. * Common Lisp Compatibility::   All known differences with Steele
  44. * Old CL Compatibility::        All known differences with old cl.el
  45. * Porting Common Lisp::         Hints for porting Common Lisp code
  46. * Function Index::
  47. * Variable Index::
  48. File: cl,  Node: Overview,  Next: Program Structure,  Prev: Top,  Up: Top
  49. Overview
  50. ********
  51. Common Lisp is a huge language, and Common Lisp systems tend to be
  52. massive and extremely complex.  Emacs Lisp, by contrast, is rather
  53. minimalist in the choice of Lisp features it offers the programmer.  As
  54. Emacs Lisp programmers have grown in number, and the applications they
  55. write have grown more ambitious, it has become clear that Emacs Lisp
  56. could benefit from many of the conveniences of Common Lisp.
  57.    The "CL" package adds a number of Common Lisp functions and control
  58. structures to Emacs Lisp.  While not a 100% complete implementation of
  59. Common Lisp, "CL" adds enough functionality to make Emacs Lisp
  60. programming significantly more convenient.
  61.    Some Common Lisp features have been omitted from this package for
  62. various reasons:
  63.    * Some features are too complex or bulky relative to their benefit
  64.      to Emacs Lisp programmers.  CLOS and Common Lisp streams are fine
  65.      examples of this group.
  66.    * Other features cannot be implemented without modification to the
  67.      Emacs Lisp interpreter itself, such as multiple return values,
  68.      lexical scoping, case-insensitive symbols, and complex numbers.
  69.      The "CL" package generally makes no attempt to emulate these
  70.      features.
  71.    * Some features conflict with existing things in Emacs Lisp.  For
  72.      example, Emacs' `assoc' function is incompatible with the Common
  73.      Lisp `assoc'.  In such cases, this package usually adds the suffix
  74.      `*' to the function name of the Common Lisp version of the
  75.      function (e.g., `assoc*').
  76.    The package described here was written by Dave Gillespie,
  77. `daveg@synaptics.com'.  It is a total rewrite of the original 1986
  78. `cl.el' package by Cesar Quiroz.  Most features of the the Quiroz
  79. package have been retained; any incompatibilities are noted in the
  80. descriptions below.  Care has been taken in this version to ensure that
  81. each function is defined efficiently, concisely, and with minimal
  82. impact on the rest of the Emacs environment.
  83. * Menu:
  84. * Usage::                How to use the CL package
  85. * Organization::         The package's five component files
  86. * Installation::         Compiling and installing CL
  87. * Naming Conventions::   Notes on CL function names
  88. File: cl,  Node: Usage,  Next: Organization,  Prev: Overview,  Up: Overview
  89. Usage
  90. =====
  91. Lisp code that uses features from the "CL" package should include at
  92. the beginning:
  93.      (require 'cl)
  94. If you want to ensure that the new (Gillespie) version of "CL" is the
  95. one that is present, add an additional `(require 'cl-19)' call:
  96.      (require 'cl)
  97.      (require 'cl-19)
  98. The second call will fail (with "`cl-19.el' not found") if the old
  99. `cl.el' package was in use.
  100.    It is safe to arrange to load "CL" at all times, e.g., in your
  101. `.emacs' file.  But it's a good idea, for portability, to `(require
  102. 'cl)' in your code even if you do this.
  103. File: cl,  Node: Organization,  Next: Installation,  Prev: Usage,  Up: Overview
  104. Organization
  105. ============
  106. The Common Lisp package is organized into four files:
  107. `cl.el'
  108.      This is the "main" file, which contains basic functions and
  109.      information about the package.  This file is relatively
  110.      compact--about 700 lines.
  111. `cl-extra.el'
  112.      This file contains the larger, more complex or unusual functions.
  113.      It is kept separate so that packages which only want to use Common
  114.      Lisp fundamentals like the `cadr' function won't need to pay the
  115.      overhead of loading the more advanced functions.
  116. `cl-seq.el'
  117.      This file contains most of the advanced functions for operating on
  118.      sequences or lists, such as `delete-if' and `assoc*'.
  119. `cl-macs.el'
  120.      This file contains the features of the packages which are macros
  121.      instead of functions.  Macros expand when the caller is compiled,
  122.      not when it is run, so the macros generally only need to be
  123.      present when the byte-compiler is running (or when the macros are
  124.      used in uncompiled code such as a `.emacs' file).  Most of the
  125.      macros of this package are isolated in `cl-macs.el' so that they
  126.      won't take up memory unless you are compiling.
  127.    The file `cl.el' includes all necessary `autoload' commands for the
  128. functions and macros in the other three files.  All you have to do is
  129. `(require 'cl)', and `cl.el' will take care of pulling in the other
  130. files when they are needed.
  131.    There is another file, `cl-compat.el', which defines some routines
  132. from the older `cl.el' package that are no longer present in the new
  133. package.  This includes internal routines like `setelt' and
  134. `zip-lists', deprecated features like `defkeyword', and an emulation of
  135. the old-style multiple-values feature.  *Note Old CL Compatibility::.
  136. File: cl,  Node: Installation,  Next: Naming Conventions,  Prev: Organization,  Up: Overview
  137. Installation
  138. ============
  139. Installation of the "CL" package is simple:  Just put the byte-compiled
  140. files `cl.elc', `cl-extra.elc', `cl-seq.elc', `cl-macs.elc', and
  141. `cl-compat.elc' into a directory on your `load-path'.
  142.    There are no special requirements to compile this package: The files
  143. do not have to be loaded before they are compiled, nor do they need to
  144. be compiled in any particular order.
  145.    You may choose to put the files into your main `lisp/' directory,
  146. replacing the original `cl.el' file there.  Or, you could put them into
  147. a directory that comes before `lisp/' on your `load-path' so that the
  148. old `cl.el' is effectively hidden.
  149.    Also, format the `cl.texinfo' file and put the resulting Info files
  150. in the `info/' directory or another suitable place.
  151.    You may instead wish to leave this package's components all in their
  152. own directory, and then add this directory to your `load-path' and
  153. (Emacs 19 only) `Info-directory-list'.  Add the directory to the front
  154. of the list so the old "CL" package and its documentation are hidden.
  155. File: cl,  Node: Naming Conventions,  Prev: Installation,  Up: Overview
  156. Naming Conventions
  157. ==================
  158. Except where noted, all functions defined by this package have the same
  159. names and calling conventions as their Common Lisp counterparts.
  160.    Following is a complete list of functions whose names were changed
  161. from Common Lisp, usually to avoid conflicts with Emacs.  In each case,
  162. a `*' has been appended to the Common Lisp name to obtain the Emacs
  163. name:
  164.      defun*        defsubst*     defmacro*     function*
  165.      member*       assoc*        rassoc*       get*
  166.      remove*       delete*       mapcar*       sort*
  167.      floor*        ceiling*      truncate*     round*
  168.      mod*          rem*          random*
  169.    Internal function and variable names in the package are prefixed by
  170. `cl-'.  Here is a complete list of functions *not* prefixed by `cl-'
  171. which were not taken from Common Lisp:
  172.      member        delete        remove        remq
  173.      rassoc        floatp-safe   lexical-let   lexical-let*
  174.      callf         callf2        letf          letf*
  175.      defsubst*     defalias      add-hook      eval-when-compile
  176. (Most of these are Emacs 19 features provided to Emacs 18 users, or
  177. introduced, like `remq', for reasons of symmetry with similar features.)
  178.    The following simple functions and macros are defined in `cl.el';
  179. they do not cause other components like `cl-extra' to be loaded.
  180.      eql           floatp-safe   abs           endp
  181.      evenp         oddp          plusp         minusp
  182.      last          butlast       nbutlast      caar .. cddddr
  183.      list*         ldiff         rest          first .. tenth
  184.      member [1]    copy-list     subst         mapcar* [2]
  185.      adjoin [3]    acons         pairlis       when
  186.      unless        pop [4]       push [4]      pushnew [3,4]
  187.      incf [4]      decf [4]      proclaim      declaim
  188.      add-hook
  189. [1] This is the Emacs 19-compatible function, not `member*'.
  190. [2] Only for one sequence argument or two list arguments.
  191. [3] Only if `:test' is `eq', `equal', or unspecified, and `:key' is not
  192. used.
  193. [4] Only when PLACE is a plain variable name.
  194. File: cl,  Node: Program Structure,  Next: Predicates,  Prev: Overview,  Up: Top
  195. Program Structure
  196. *****************
  197. This section describes features of the "CL" package which have to do
  198. with programs as a whole: advanced argument lists for functions, and
  199. the `eval-when' construct.
  200. * Menu:
  201. * Argument Lists::       `&key', `&aux', `defun*', `defmacro*'.
  202. * Time of Evaluation::   The `eval-when' construct.
  203. * Function Aliases::     The `defalias' function.
  204. File: cl,  Node: Argument Lists,  Next: Time of Evaluation,  Prev: Program Structure,  Up: Program Structure
  205. Argument Lists
  206. ==============
  207. Emacs Lisp's notation for argument lists of functions is a subset of
  208. the Common Lisp notation.  As well as the familiar `&optional' and
  209. `&rest' markers, Common Lisp allows you to specify default values for
  210. optional arguments, and it provides the additional markers `&key' and
  211. `&aux'.
  212.    Since argument parsing is built-in to Emacs, there is no way for
  213. this package to implement Common Lisp argument lists seamlessly.
  214. Instead, this package defines alternates for several Lisp forms which
  215. you must use if you need Common Lisp argument lists.
  216.  - Special Form: defun* NAME ARGLIST BODY...
  217.      This form is identical to the regular `defun' form, except that
  218.      ARGLIST is allowed to be a full Common Lisp argument list.  Also,
  219.      the function body is enclosed in an implicit block called NAME;
  220.      *note Blocks and Exits::..
  221.  - Special Form: defsubst* NAME ARGLIST BODY...
  222.      This is just like `defun*', except that the function that is
  223.      defined is automatically proclaimed `inline', i.e., calls to it
  224.      may be expanded into in-line code by the byte compiler.  This is
  225.      analogous to the `defsubst' form in Emacs 19; `defsubst*' uses a
  226.      different method (compiler macros) which works in all version of
  227.      Emacs, and also generates somewhat more efficient inline
  228.      expansions.  In particular, `defsubst*' arranges for the
  229.      processing of keyword arguments, default values, etc., to be done
  230.      at compile-time whenever possible.
  231.  - Special Form: defmacro* NAME ARGLIST BODY...
  232.      This is identical to the regular `defmacro' form, except that
  233.      ARGLIST is allowed to be a full Common Lisp argument list.  The
  234.      `&environment' keyword is supported as described in Steele.  The
  235.      `&whole' keyword is supported only within destructured lists (see
  236.      below); top-level `&whole' cannot be implemented with the current
  237.      Emacs Lisp interpreter.  The macro expander body is enclosed in an
  238.      implicit block called NAME.
  239.  - Special Form: function* SYMBOL-OR-LAMBDA
  240.      This is identical to the regular `function' form, except that if
  241.      the argument is a `lambda' form then that form may use a full
  242.      Common Lisp argument list.
  243.    Also, all forms (such as `defsetf' and `flet') defined in this
  244. package that include ARGLISTs in their syntax allow full Common Lisp
  245. argument lists.
  246.    Note that it is *not* necessary to use `defun*' in order to have
  247. access to most "CL" features in your function.  These features are
  248. always present; `defun*''s only difference from `defun' is its more
  249. flexible argument lists and its implicit block.
  250.    The full form of a Common Lisp argument list is
  251.      (VAR...
  252.       &optional (VAR INITFORM SVAR)...
  253.       &rest VAR
  254.       &key ((KEYWORD VAR) INITFORM SVAR)...
  255.       &aux (VAR INITFORM)...)
  256.    Each of the five argument list sections is optional.  The SVAR,
  257. INITFORM, and KEYWORD parts are optional; if they are omitted, then
  258. `(VAR)' may be written simply `VAR'.
  259.    The first section consists of zero or more "required" arguments.
  260. These arguments must always be specified in a call to the function;
  261. there is no difference between Emacs Lisp and Common Lisp as far as
  262. required arguments are concerned.
  263.    The second section consists of "optional" arguments.  These
  264. arguments may be specified in the function call; if they are not,
  265. INITFORM specifies the default value used for the argument.  (No
  266. INITFORM means to use `nil' as the default.)  The INITFORM is evaluated
  267. with the bindings for the preceding arguments already established; `(a
  268. &optional (b (1+ a)))' matches one or two arguments, with the second
  269. argument defaulting to one plus the first argument.  If the SVAR is
  270. specified, it is an auxiliary variable which is bound to `t' if the
  271. optional argument was specified, or to `nil' if the argument was
  272. omitted.  If you don't use an SVAR, then there will be no way for your
  273. function to tell whether it was called with no argument, or with the
  274. default value passed explicitly as an argument.
  275.    The third section consists of a single "rest" argument.  If more
  276. arguments were passed to the function than are accounted for by the
  277. required and optional arguments, those extra arguments are collected
  278. into a list and bound to the "rest" argument variable.  Common Lisp's
  279. `&rest' is equivalent to that of Emacs Lisp.  Common Lisp accepts
  280. `&body' as a synonym for `&rest' in macro contexts; this package
  281. accepts it all the time.
  282.    The fourth section consists of "keyword" arguments.  These are
  283. optional arguments which are specified by name rather than positionally
  284. in the argument list.  For example,
  285.      (defun* foo (a &optional b &key c d (e 17)))
  286. defines a function which may be called with one, two, or more
  287. arguments.  The first two arguments are bound to `a' and `b' in the
  288. usual way.  The remaining arguments must be pairs of the form `:c',
  289. `:d', or `:e' followed by the value to be bound to the corresponding
  290. argument variable.  (Symbols whose names begin with a colon are called
  291. "keywords", and they are self-quoting in the same way as `nil' and `t'.)
  292.    For example, the call `(foo 1 2 :d 3 :c 4)' sets the five arguments
  293. to 1, 2, 4, 3, and 17, respectively.  If the same keyword appears more
  294. than once in the function call, the first occurrence takes precedence
  295. over the later ones.  Note that it is not possible to specify keyword
  296. arguments without specifying the optional argument `b' as well, since
  297. `(foo 1 :c 2)' would bind `b' to the keyword `:c', then signal an error
  298. because `2' is not a valid keyword.
  299.    If a KEYWORD symbol is explicitly specified in the argument list as
  300. shown in the above diagram, then that keyword will be used instead of
  301. just the variable name prefixed with a colon.  You can specify a
  302. KEYWORD symbol which does not begin with a colon at all, but such
  303. symbols will not be self-quoting; you will have to quote them
  304. explicitly with an apostrophe in the function call.
  305.    Ordinarily it is an error to pass an unrecognized keyword to a
  306. function, e.g., `(foo 1 2 :c 3 :goober 4)'.  You can ask Lisp to ignore
  307. unrecognized keywords, either by adding the marker `&allow-other-keys'
  308. after the keyword section of the argument list, or by specifying an
  309. `:allow-other-keys' argument in the call whose value is non-`nil'.  If
  310. the function uses both `&rest' and `&key' at the same time, the "rest"
  311. argument is bound to the keyword list as it appears in the call.  For
  312. example:
  313.      (defun* find-thing (thing &rest rest &key need &allow-other-keys)
  314.        (or (apply 'member* thing thing-list :allow-other-keys t rest)
  315.            (if need (error "Thing not found"))))
  316. This function takes a `:need' keyword argument, but also accepts other
  317. keyword arguments which are passed on to the `member*' function.
  318. `allow-other-keys' is used to keep both `find-thing' and `member*' from
  319. complaining about each others' keywords in the arguments.
  320.    In Common Lisp, keywords are recognized by the Lisp parser itself
  321. and treated as special entities.  In Emacs, keywords are just symbols
  322. whose names begin with colons, which `defun*' has arranged to set equal
  323. to themselves so that they will essentially be self-quoting.
  324.    As a (significant) performance optimization, this package implements
  325. the scan for keyword arguments by calling `memq' to search for keywords
  326. in a "rest" argument.  Technically speaking, this is incorrect, since
  327. `memq' looks at the odd-numbered values as well as the even-numbered
  328. keywords.  The net effect is that if you happen to pass a keyword symbol
  329. as the *value* of another keyword argument, where that keyword symbol
  330. happens to equal the name of a valid keyword argument of the same
  331. function, then the keyword parser will become confused.  This minor bug
  332. can only affect you if you use keyword symbols as general-purpose data
  333. in your program; this practice is strongly discouraged in Emacs Lisp.
  334.    The fifth section of the argument list consists of "auxiliary
  335. variables".  These are not really arguments at all, but simply
  336. variables which are bound to `nil' or to the specified INITFORMS during
  337. execution of the function.  There is no difference between the
  338. following two functions, except for a matter of stylistic taste:
  339.      (defun* foo (a b &aux (c (+ a b)) d)
  340.        BODY)
  341.      
  342.      (defun* foo (a b)
  343.        (let ((c (+ a b)) d)
  344.          BODY))
  345.    Argument lists support "destructuring".  In Common Lisp,
  346. destructuring is only allowed with `defmacro'; this package allows it
  347. with `defun*' and other argument lists as well.  In destructuring, any
  348. argument variable (VAR in the above diagram) can be replaced by a list
  349. of variables, or more generally, a recursive argument list.  The
  350. corresponding argument value must be a list whose elements match this
  351. recursive argument list.  For example:
  352.      (defmacro* dolist ((var listform &optional resultform)
  353.                         &rest body)
  354.        ...)
  355.    This says that the first argument of `dolist' must be a list of two
  356. or three items; if there are other arguments as well as this list, they
  357. are stored in `body'.  All features allowed in regular argument lists
  358. are allowed in these recursive argument lists.  In addition, the clause
  359. `&whole VAR' is allowed at the front of a recursive argument list.  It
  360. binds VAR to the whole list being matched; thus `(&whole all a b)'
  361. matches a list of two things, with `a' bound to the first thing, `b'
  362. bound to the second thing, and `all' bound to the list itself.  (Common
  363. Lisp allows `&whole' in top-level `defmacro' argument lists as well,
  364. but Emacs Lisp does not support this usage.)
  365.    One last feature of destructuring is that the argument list may be
  366. dotted, so that the argument list `(a b . c)' is functionally
  367. equivalent to `(a b &rest c)'.
  368.    If the optimization quality `safety' is set to 0 (*note
  369. Declarations::.), error checking for wrong number of arguments and
  370. invalid keyword arguments is disabled.  By default, argument lists are
  371. rigorously checked.
  372. File: cl,  Node: Time of Evaluation,  Next: Function Aliases,  Prev: Argument Lists,  Up: Program Structure
  373. Time of Evaluation
  374. ==================
  375. Normally, the byte-compiler does not actually execute the forms in a
  376. file it compiles.  For example, if a file contains `(setq foo t)', the
  377. act of compiling it will not actually set `foo' to `t'.  This is true
  378. even if the `setq' was a top-level form (i.e., not enclosed in a
  379. `defun' or other form).  Sometimes, though, you would like to have
  380. certain top-level forms evaluated at compile-time.  For example, the
  381. compiler effectively evaluates `defmacro' forms at compile-time so that
  382. later parts of the file can refer to the macros that are defined.
  383.  - Special Form: eval-when (SITUATIONS...) FORMS...
  384.      This form controls when the body FORMS are evaluated.  The
  385.      SITUATIONS list may contain any set of the symbols `compile',
  386.      `load', and `eval' (or their long-winded ANSI equivalents,
  387.      `:compile-toplevel', `:load-toplevel', and `:execute').
  388.      The `eval-when' form is handled differently depending on whether
  389.      or not it is being compiled as a top-level form.  Specifically, it
  390.      gets special treatment if it is being compiled by a command such
  391.      as `byte-compile-file' which compiles files or buffers of code,
  392.      and it appears either literally at the top level of the file or
  393.      inside a top-level `progn'.
  394.      For compiled top-level `eval-when's, the body FORMS are executed
  395.      at compile-time if `compile' is in the SITUATIONS list, and the
  396.      FORMS are written out to the file (to be executed at load-time) if
  397.      `load' is in the SITUATIONS list.
  398.      For non-compiled-top-level forms, only the `eval' situation is
  399.      relevant.  (This includes forms executed by the interpreter, forms
  400.      compiled with `byte-compile' rather than `byte-compile-file', and
  401.      non-top-level forms.)  The `eval-when' acts like a `progn' if
  402.      `eval' is specified, and like `nil' (ignoring the body FORMS) if
  403.      not.
  404.      The rules become more subtle when `eval-when's are nested; consult
  405.      Steele (second edition) for the gruesome details (and some
  406.      gruesome examples).
  407.      Some simple examples:
  408.           ;; Top-level forms in foo.el:
  409.           (eval-when (compile)           (setq foo1 'bar))
  410.           (eval-when (load)              (setq foo2 'bar))
  411.           (eval-when (compile load)      (setq foo3 'bar))
  412.           (eval-when (eval)              (setq foo4 'bar))
  413.           (eval-when (eval compile)      (setq foo5 'bar))
  414.           (eval-when (eval load)         (setq foo6 'bar))
  415.           (eval-when (eval compile load) (setq foo7 'bar))
  416.      When `foo.el' is compiled, these variables will be set during the
  417.      compilation itself:
  418.           foo1  foo3  foo5  foo7      ; `compile'
  419.      When `foo.elc' is loaded, these variables will be set:
  420.           foo2  foo3  foo6  foo7      ; `load'
  421.      And if `foo.el' is loaded uncompiled, these variables will be set:
  422.           foo4  foo5  foo6  foo7      ; `eval'
  423.      If these seven `eval-when's had been, say, inside a `defun', then
  424.      the first three would have been equivalent to `nil' and the last
  425.      four would have been equivalent to the corresponding `setq's.
  426.      Note that `(eval-when (load eval) ...)' is equivalent to `(progn
  427.      ...)' in all contexts.  The compiler treats certain top-level
  428.      forms, like `defmacro' (sort-of) and `require', as if they were
  429.      wrapped in `(eval-when (compile load eval) ...)'.
  430.    Emacs 19 includes two special forms related to `eval-when'.  One of
  431. these, `eval-when-compile', is not quite equivalent to any `eval-when'
  432. construct and is described below.  This package defines a version of
  433. `eval-when-compile' for the benefit of Emacs 18 users.
  434.    The other form, `(eval-and-compile ...)', is exactly equivalent to
  435. `(eval-when (compile load eval) ...)' and so is not itself defined by
  436. this package.
  437.  - Special Form: eval-when-compile FORMS...
  438.      The FORMS are evaluated at compile-time; at execution time, this
  439.      form acts like a quoted constant of the resulting value.  Used at
  440.      top-level, `eval-when-compile' is just like `eval-when (compile
  441.      eval)'.  In other contexts, `eval-when-compile' allows code to be
  442.      evaluated once at compile-time for efficiency or other reasons.
  443.      This form is similar to the `#.' syntax of true Common Lisp.
  444.  - Special Form: load-time-value FORM
  445.      The FORM is evaluated at load-time; at execution time, this form
  446.      acts like a quoted constant of the resulting value.
  447.      Early Common Lisp had a `#,' syntax that was similar to this, but
  448.      ANSI Common Lisp replaced it with `load-time-value' and gave it
  449.      more well-defined semantics.
  450.      In a compiled file, `load-time-value' arranges for FORM to be
  451.      evaluated when the `.elc' file is loaded and then used as if it
  452.      were a quoted constant.  In code compiled by `byte-compile' rather
  453.      than `byte-compile-file', the effect is identical to
  454.      `eval-when-compile'.  In uncompiled code, both `eval-when-compile'
  455.      and `load-time-value' act exactly like `progn'.
  456.           (defun report ()
  457.             (insert "This function was executed on: "
  458.                     (current-time-string)
  459.                     ", compiled on: "
  460.                     (eval-when-compile (current-time-string))
  461.                     ;; or '#.(current-time-string) in real Common Lisp
  462.                     ", and loaded on: "
  463.                     (load-time-value (current-time-string))))
  464.      Byte-compiled, the above defun will result in the following code
  465.      (or its compiled equivalent, of course) in the `.elc' file:
  466.           (setq --temp-- (current-time-string))
  467.           (defun report ()
  468.             (insert "This function was executed on: "
  469.                     (current-time-string)
  470.                     ", compiled on: "
  471.                     '"Wed Jun 23 18:33:43 1993"
  472.                     ", and loaded on: "
  473.                     --temp--))
  474. File: cl,  Node: Function Aliases,  Prev: Time of Evaluation,  Up: Program Structure
  475. Function Aliases
  476. ================
  477. This section describes a feature from GNU Emacs 19 which this package
  478. makes available in other versions of Emacs.
  479.  - Function: defalias SYMBOL FUNCTION
  480.      This function sets SYMBOL's function cell to FUNCTION.  It is
  481.      equivalent to `fset', except that in GNU Emacs 19 it also records
  482.      the setting in `load-history' so that it can be undone by a later
  483.      `unload-feature'.
  484.      In other versions of Emacs, `defalias' is a synonym for `fset'.
  485. File: cl,  Node: Predicates,  Next: Control Structure,  Prev: Program Structure,  Up: Top
  486. Predicates
  487. **********
  488. This section describes functions for testing whether various facts are
  489. true or false.
  490. * Menu:
  491. * Type Predicates::      `typep', `deftype', and `coerce'
  492. * Equality Predicates::  `eql' and `equalp'
  493. File: cl,  Node: Type Predicates,  Next: Equality Predicates,  Prev: Predicates,  Up: Predicates
  494. Type Predicates
  495. ===============
  496. The "CL" package defines a version of the Common Lisp `typep' predicate.
  497.  - Function: typep OBJECT TYPE
  498.      Check if OBJECT is of type TYPE, where TYPE is a (quoted) type
  499.      name of the sort used by Common Lisp.  For example, `(typep foo
  500.      'integer)' is equivalent to `(integerp foo)'.
  501.    The TYPE argument to the above function is either a symbol or a list
  502. beginning with a symbol.
  503.    * If the type name is a symbol, Emacs appends `-p' to the symbol
  504.      name to form the name of a predicate function for testing the
  505.      type.  (Built-in predicates whose names end in `p' rather than
  506.      `-p' are used when appropriate.)
  507.    * The type symbol `t' stands for the union of all types.  `(typep
  508.      OBJECT t)' is always true.  Likewise, the type symbol `nil' stands
  509.      for nothing at all, and `(typep OBJECT nil)' is always false.
  510.    * The type symbol `null' represents the symbol `nil'.  Thus `(typep
  511.      OBJECT 'null)' is equivalent to `(null OBJECT)'.
  512.    * The type symbol `real' is a synonym for `number', and `fixnum' is
  513.      a synonym for `integer'.
  514.    * The type symbols `character' and `string-char' match integers in
  515.      the range from 0 to 255.
  516.    * The type symbol `float' uses the `floatp-safe' predicate defined
  517.      by this package rather than `floatp', so it will work correctly
  518.      even in Emacs versions without floating-point support.
  519.    * The type list `(integer LOW HIGH)' represents all integers between
  520.      LOW and HIGH, inclusive.  Either bound may be a list of a single
  521.      integer to specify an exclusive limit, or a `*' to specify no
  522.      limit.  The type `(integer * *)' is thus equivalent to `integer'.
  523.    * Likewise, lists beginning with `float', `real', or `number'
  524.      represent numbers of that type falling in a particular range.
  525.    * Lists beginning with `and', `or', and `not' form combinations of
  526.      types.  For example, `(or integer (float 0 *))' represents all
  527.      objects that are integers or non-negative floats.
  528.    * Lists beginning with `member' or `member*' represent objects `eql'
  529.      to any of the following values.  For example, `(member 1 2 3 4)'
  530.      is equivalent to `(integer 1 4)', and `(member nil)' is equivalent
  531.      to `null'.
  532.    * Lists of the form `(satisfies PREDICATE)' represent all objects
  533.      for which PREDICATE returns true when called with that object as
  534.      an argument.
  535.    The following function and macro (not technically predicates) are
  536. related to `typep'.
  537.  - Function: coerce OBJECT TYPE
  538.      This function attempts to convert OBJECT to the specified TYPE.
  539.      If OBJECT is already of that type as determined by `typep', it is
  540.      simply returned.  Otherwise, certain types of conversions will be
  541.      made:  If TYPE is any sequence type (`string', `list', etc.) then
  542.      OBJECT will be converted to that type if possible.  If TYPE is
  543.      `character', then strings of length one and symbols with
  544.      one-character names can be coerced.  If TYPE is `float', then
  545.      integers can be coerced in versions of Emacs that support floats.
  546.      In all other circumstances, `coerce' signals an error.
  547.  - Special Form: deftype NAME ARGLIST FORMS...
  548.      This macro defines a new type called NAME.  It is similar to
  549.      `defmacro' in many ways; when NAME is encountered as a type name,
  550.      the body FORMS are evaluated and should return a type specifier
  551.      that is equivalent to the type.  The ARGLIST is a Common Lisp
  552.      argument list of the sort accepted by `defmacro*'.  The type
  553.      specifier `(NAME ARGS...)' is expanded by calling the expander
  554.      with those arguments; the type symbol `NAME' is expanded by
  555.      calling the expander with no arguments.  The ARGLIST is processed
  556.      the same as for `defmacro*' except that optional arguments without
  557.      explicit defaults use `*' instead of `nil' as the "default"
  558.      default.  Some examples:
  559.           (deftype null () '(satisfies null))    ; predefined
  560.           (deftype list () '(or null cons))      ; predefined
  561.           (deftype unsigned-byte (&optional bits)
  562.             (list 'integer 0 (if (eq bits '*) bits (1- (lsh 1 bits)))))
  563.           (unsigned-byte 8)  ==  (integer 0 255)
  564.           (unsigned-byte)  ==  (integer 0 *)
  565.           unsigned-byte  ==  (integer 0 *)
  566.      The last example shows how the Common Lisp `unsigned-byte' type
  567.      specifier could be implemented if desired; this package does not
  568.      implement `unsigned-byte' by default.
  569.    The `typecase' and `check-type' macros also use type names.  *Note
  570. Conditionals::.  *Note Assertions::.  The `map', `concatenate', and
  571. `merge' functions take type-name arguments to specify the type of
  572. sequence to return.  *Note Sequences::.
  573. File: cl,  Node: Equality Predicates,  Prev: Type Predicates,  Up: Predicates
  574. Equality Predicates
  575. ===================
  576. This package defines two Common Lisp predicates, `eql' and `equalp'.
  577.  - Function: eql A B
  578.      This function is almost the same as `eq', except that if A and B
  579.      are numbers of the same type, it compares them for numeric
  580.      equality (as if by `equal' instead of `eq').  This makes a
  581.      difference only for versions of Emacs that are compiled with
  582.      floating-point support, such as Emacs 19.  Emacs floats are
  583.      allocated objects just like cons cells, which means that `(eq 3.0
  584.      3.0)' will not necessarily be true--if the two `3.0's were
  585.      allocated separately, the pointers will be different even though
  586.      the numbers are the same.  But `(eql 3.0 3.0)' will always be true.
  587.      The types of the arguments must match, so `(eql 3 3.0)' is still
  588.      false.
  589.      Note that Emacs integers are "direct" rather than allocated, which
  590.      basically means `(eq 3 3)' will always be true.  Thus `eq' and
  591.      `eql' behave differently only if floating-point numbers are
  592.      involved, and are indistinguishable on Emacs versions that don't
  593.      support floats.
  594.      There is a slight inconsistency with Common Lisp in the treatment
  595.      of positive and negative zeros.  Some machines, notably those with
  596.      IEEE standard arithmetic, represent `+0' and `-0' as distinct
  597.      values.  Normally this doesn't matter because the standard
  598.      specifies that `(= 0.0 -0.0)' should always be true, and this is
  599.      indeed what Emacs Lisp and Common Lisp do.  But the Common Lisp
  600.      standard states that `(eql 0.0 -0.0)' and `(equal 0.0 -0.0)' should
  601.      be false on IEEE-like machines; Emacs Lisp does not do this, and in
  602.      fact the only known way to distinguish between the two zeros in
  603.      Emacs Lisp is to `format' them and check for a minus sign.
  604.  - Function: equalp A B
  605.      This function is a more flexible version of `equal'.  In
  606.      particular, it compares strings case-insensitively, and it compares
  607.      numbers without regard to type (so that `(equalp 3 3.0)' is true).
  608.      Vectors and conses are compared recursively.  All other objects
  609.      are compared as if by `equal'.
  610.      This function differs from Common Lisp `equalp' in several
  611.      respects.  First, Common Lisp's `equalp' also compares
  612.      *characters* case-insensitively, which would be impractical in
  613.      this package since Emacs does not distinguish between integers and
  614.      characters.  In keeping with the idea that strings are less
  615.      vector-like in Emacs Lisp, this package's `equalp' also will not
  616.      compare strings against vectors of integers.  Finally, Common
  617.      Lisp's `equalp' compares hash tables without regard to ordering,
  618.      whereas this package simply compares hash tables in terms of their
  619.      underlying structure (which means vectors for Lucid Emacs 19 hash
  620.      tables, or lists for other hash tables).
  621.    Also note that the Common Lisp functions `member' and `assoc' use
  622. `eql' to compare elements, whereas Emacs Lisp follows the MacLisp
  623. tradition and uses `equal' for these two functions.  In Emacs, use
  624. `member*' and `assoc*' to get functions which use `eql' for comparisons.
  625. File: cl,  Node: Control Structure,  Next: Macros,  Prev: Predicates,  Up: Top
  626. Control Structure
  627. *****************
  628. The features described in the following sections implement various
  629. advanced control structures, including the powerful `setf' facility and
  630. a number of looping and conditional constructs.
  631. * Menu:
  632. * Assignment::             The `psetq' form
  633. * Generalized Variables::  `setf', `incf', `push', etc.
  634. * Variable Bindings::      `progv', `lexical-let', `flet', `macrolet'
  635. * Conditionals::           `when', `unless', `case', `typecase'
  636. * Blocks and Exits::       `block', `return', `return-from'
  637. * Iteration::              `do', `dotimes', `dolist', `do-symbols'
  638. * Loop Facility::          The Common Lisp `loop' macro
  639. * Multiple Values::        `values', `multiple-value-bind', etc.
  640. File: cl,  Node: Assignment,  Next: Generalized Variables,  Prev: Control Structure,  Up: Control Structure
  641. Assignment
  642. ==========
  643. The `psetq' form is just like `setq', except that multiple assignments
  644. are done in parallel rather than sequentially.
  645.  - Special Form: psetq [SYMBOL FORM]...
  646.      This special form (actually a macro) is used to assign to several
  647.      variables simultaneously.  Given only one SYMBOL and FORM, it has
  648.      the same effect as `setq'.  Given several SYMBOL and FORM pairs,
  649.      it evaluates all the FORMs in advance and then stores the
  650.      corresponding variables afterwards.
  651.           (setq x 2 y 3)
  652.           (setq x (+ x y)  y (* x y))
  653.           x
  654.                => 5
  655.           y                     ; `y' was computed after `x' was set.
  656.                => 15
  657.           (setq x 2 y 3)
  658.           (psetq x (+ x y)  y (* x y))
  659.           x
  660.                => 5
  661.           y                     ; `y' was computed before `x' was set.
  662.                => 6
  663.      The simplest use of `psetq' is `(psetq x y y x)', which exchanges
  664.      the values of two variables.  (The `rotatef' form provides an even
  665.      more convenient way to swap two variables; *note Modify Macros::..)
  666.      `psetq' always returns `nil'.
  667. File: cl,  Node: Generalized Variables,  Next: Variable Bindings,  Prev: Assignment,  Up: Control Structure
  668. Generalized Variables
  669. =====================
  670. A "generalized variable" or "place form" is one of the many places in
  671. Lisp memory where values can be stored.  The simplest place form is a
  672. regular Lisp variable.  But the cars and cdrs of lists, elements of
  673. arrays, properties of symbols, and many other locations are also places
  674. where Lisp values are stored.
  675.    The `setf' form is like `setq', except that it accepts arbitrary
  676. place forms on the left side rather than just symbols.  For example,
  677. `(setf (car a) b)' sets the car of `a' to `b', doing the same operation
  678. as `(setcar a b)' but without having to remember two separate functions
  679. for setting and accessing every type of place.
  680.    Generalized variables are analogous to "lvalues" in the C language,
  681. where `x = a[i]' gets an element from an array and `a[i] = x' stores an
  682. element using the same notation.  Just as certain forms like `a[i]' can
  683. be lvalues in C, there is a set of forms that can be generalized
  684. variables in Lisp.
  685. * Menu:
  686. * Basic Setf::         `setf' and place forms
  687. * Modify Macros::      `incf', `push', `rotatef', `letf', `callf', etc.
  688. * Customizing Setf::   `define-modify-macro', `defsetf', `define-setf-method'
  689. File: cl,  Node: Basic Setf,  Next: Modify Macros,  Prev: Generalized Variables,  Up: Generalized Variables
  690. Basic Setf
  691. ----------
  692. The `setf' macro is the most basic way to operate on generalized
  693. variables.
  694.  - Special Form: setf [PLACE FORM]...
  695.      This macro evaluates FORM and stores it in PLACE, which must be a
  696.      valid generalized variable form.  If there are several PLACE and
  697.      FORM pairs, the assignments are done sequentially just as with
  698.      `setq'.  `setf' returns the value of the last FORM.
  699.      The following Lisp forms will work as generalized variables, and
  700.      so may legally appear in the PLACE argument of `setf':
  701.         * A symbol naming a variable.  In other words, `(setf x y)' is
  702.           exactly equivalent to `(setq x y)', and `setq' itself is
  703.           strictly speaking redundant now that `setf' exists.  Many
  704.           programmers continue to prefer `setq' for setting simple
  705.           variables, though, purely for stylistic or historical reasons.
  706.           The macro `(setf x y)' actually expands to `(setq x y)', so
  707.           there is no performance penalty for using it in compiled code.
  708.         * A call to any of the following Lisp functions:
  709.                car                 cdr                 caar .. cddddr
  710.                nth                 rest                first .. tenth
  711.                aref                elt                 nthcdr
  712.                symbol-function     symbol-value        symbol-plist
  713.                get                 get*                getf
  714.                gethash             subseq
  715.           Note that for `nthcdr' and `getf', the list argument of the
  716.           function must itself be a valid PLACE form.  For example,
  717.           `(setf (nthcdr 0 foo) 7)' will set `foo' itself to 7.  Note
  718.           that `push' and `pop' on an `nthcdr' place can be used to
  719.           insert or delete at any position in a list.  The use of
  720.           `nthcdr' as a PLACE form is an extension to standard Common
  721.           Lisp.
  722.         * The following Emacs-specific functions are also `setf'-able.
  723.           (Some of these are defined only in Emacs 19 or only in Lucid
  724.           Emacs.)
  725.                buffer-file-name                  marker-position
  726.                buffer-modified-p                 match-data
  727.                buffer-name                       mouse-position
  728.                buffer-string                     overlay-end
  729.                buffer-substring                  overlay-get
  730.                current-buffer                    overlay-start
  731.                current-case-table                point
  732.                current-column                    point-marker
  733.                current-global-map                point-max
  734.                current-input-mode                point-min
  735.                current-local-map                 process-buffer
  736.                current-window-configuration      process-filter
  737.                default-file-modes                process-sentinel
  738.                default-value                     read-mouse-position
  739.                documentation-property            screen-height
  740.                extent-data                       screen-menubar
  741.                extent-end-position               screen-width
  742.                extent-start-position             selected-window
  743.                face-background                   selected-screen
  744.                face-background-pixmap            selected-frame
  745.                face-font                         standard-case-table
  746.                face-foreground                   syntax-table
  747.                face-underline-p                  window-buffer
  748.                file-modes                        window-dedicated-p
  749.                frame-height                      window-display-table
  750.                frame-parameters                  window-height
  751.                frame-visible-p                   window-hscroll
  752.                frame-width                       window-point
  753.                get-register                      window-start
  754.                getenv                            window-width
  755.                global-key-binding                x-get-cut-buffer
  756.                keymap-parent                     x-get-cutbuffer
  757.                local-key-binding                 x-get-secondary-selection
  758.                mark                              x-get-selection
  759.                mark-marker
  760.           Most of these have directly corresponding "set" functions,
  761.           like `use-local-map' for `current-local-map', or `goto-char'
  762.           for `point'.  A few, like `point-min', expand to longer
  763.           sequences of code when they are `setf''d (`(narrow-to-region
  764.           x (point-max))' in this case).
  765.         * A call of the form `(substring SUBPLACE N [M])', where
  766.           SUBPLACE is itself a legal generalized variable whose current
  767.           value is a string, and where the value stored is also a
  768.           string.  The new string is spliced into the specified part of
  769.           the destination string.  For example:
  770.                (setq a (list "hello" "world"))
  771.                     => ("hello" "world")
  772.                (cadr a)
  773.                     => "world"
  774.                (substring (cadr a) 2 4)
  775.                     => "rl"
  776.                (setf (substring (cadr a) 2 4) "o")
  777.                     => "o"
  778.                (cadr a)
  779.                     => "wood"
  780.                a
  781.                     => ("hello" "wood")
  782.           The generalized variable `buffer-substring', listed above,
  783.           also works in this way by replacing a portion of the current
  784.           buffer.
  785.         * A call of the form `(apply 'FUNC ...)' or `(apply (function
  786.           FUNC) ...)', where FUNC is a `setf'-able function whose store
  787.           function is "suitable" in the sense described in Steele's
  788.           book; since none of the standard Emacs place functions are
  789.           suitable in this sense, this feature is only interesting when
  790.           used with places you define yourself with
  791.           `define-setf-method' or the long form of `defsetf'.
  792.         * A macro call, in which case the macro is expanded and `setf'
  793.           is applied to the resulting form.
  794.         * Any form for which a `defsetf' or `define-setf-method' has
  795.           been made.
  796.      Using any forms other than these in the PLACE argument to `setf'
  797.      will signal an error.
  798.      The `setf' macro takes care to evaluate all subforms in the proper
  799.      left-to-right order; for example,
  800.           (setf (aref vec (incf i)) i)
  801.      looks like it will evaluate `(incf i)' exactly once, before the
  802.      following access to `i'; the `setf' expander will insert temporary
  803.      variables as necessary to ensure that it does in fact work this
  804.      way no matter what setf-method is defined for `aref'.  (In this
  805.      case, `aset' would be used and no such steps would be necessary
  806.      since `aset' takes its arguments in a convenient order.)
  807.      However, if the PLACE form is a macro which explicitly evaluates
  808.      its arguments in an unusual order, this unusual order will be
  809.      preserved.  Adapting an example from Steele, given
  810.           (defmacro wrong-order (x y) (list 'aref y x))
  811.      the form `(setf (wrong-order A B) 17)' will evaluate B first, then
  812.      A, just as in an actual call to `wrong-order'.
  813.